//
// Created by spiro on 19-3-25.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

void InitializeList(List * plist) {
    *plist = NULL;
}

bool ListIsEmpty(const List * plist) {
    return (*plist == NULL);
}

bool ListIsFull(const List * plist) {
    Node * pt;
    bool full;
    pt = (Node *) malloc(sizeof(Node));
    if (pt == NULL) {
        full = true;
    } else {
        full = false;
    }
    free(pt);
    return full;
}

unsigned int ListItemCount(const List * plist) {
    Node * temp = *plist;
    unsigned int count = 0;

    while (temp != NULL) {
        count++;
        temp = temp->next;
    }

    return count;
}

bool AddItem(Item item, List * plist) {

    Node * new_node = (Node *) malloc(sizeof(Node));
    if (new_node == NULL) {
        return false;
    }

    new_node->item = item;
    new_node->next = NULL;

    if (*plist == NULL) {
        *plist = new_node;
    } else {
        Node * temp = *plist;
        while (temp->next != NULL) {
            temp = temp->next;
        }

        temp->next = new_node;
    }

    return true;
}

void Traverse(const List * plist, void (* pfun)(Item item)) {
    Node * temp = *plist;

    while (temp != NULL) {
        (* pfun)(temp->item);
        temp = temp->next;
    }
}

void EmptyTheList(List * plist) {
    Node * temp = *plist;

    while (temp != NULL) {
        *plist = (*plist)->next;
        free(temp);
        temp = *plist;
    }
}